<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>登录</title>
  <link href="css/admin_login.css" rel="stylesheet" type="text/css" />
  <script>
    function validateForm() {
      var aphone = document.getElementById('phone').value;
      var apassword = document.getElementById('password').value;
      if (aphone == "" || apassword == "") {
        alert("手机号和密码不能为空");
        return false;
      }
	  const data = {
		  phone: aphone,
		  password: apassword
		  
	  }
	  const jsonData = JSON.stringify(data); 
	  fetch("http://localhost:8080/consumer/adminLogin",{method: 'POST',
			headers: {  
			    'Content-Type': 'application/json',
			  }, 
			body: jsonData
			})
	  
	      .then(response => {
	          // 确保服务器返回的是JSON数据
	          if (!response.ok) {
	              throw new Error('Failed to fetch data');
	          }
	          return response.json(); // 将response转换为JSON
	      })
	      .then(data => {
	          var token = data.token;
			  sessionStorage.setItem("token",token);
			  sessionStorage.setItem("consumerName",data.consumerName);
			  sessionStorage.setItem("role",data.role);
			  sessionStorage.setItem("store",JSON.stringify(data.store));
			  sessionStorage.setItem("phone",data.phone);
			  sessionStorage.setItem("email",data.email);
			  var role = "店长";
			  if(data.role > 1){
				  role = "店员";
			  }
			  document.getElementById("res").innerText="欢迎"+role+":"+data.consumerName;
			  window.location.href = "/shop/admin.html";
			  
	          
	      })
	      .catch(error => {
			  document.getElementById("res").innerText="401,账号或密码错误";
	          // 处理错误
	          console.error('Error:', error);
	      });
    }
  </script>
</head>
<body>
<div class="admin_login_wrap">
  <h1 style="text-align: center;">商家登录</h1>
  <div class="adming_login_border">
    <div class="admin_input">
        <ul class="admin_items">
          <li>
            <span style="color:red" id="res"></span>
          </li>
          <li>
            <label for="phone">手机号：</label>
            <input type="text" name="phone" value="" id="phone" size="40" class="admin_input_style" />
          </li>
          <li>
            <label for="password">密码：</label>
            <input type="password" name="password" value="" id="password" size="40" class="admin_input_style" />
          </li>
          <li>
            <input type="submit" tabindex="3" value="提交" class="btn btn-primary" onclick="validateForm()"/>
          </li>
        </ul>
    </div>
  </div>
</div>
</body>
</html>
